package com.wjq.demo.springmvc;

import org.springframework.lang.Nullable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * @author wjq
 * @since 2023-03-01
 */
@RestController
@Validated
public class TestController {


    @GetMapping("/validate")
    @ResponseBody
    @Validated
    public String validate(@Valid @Size(min = 1, max = 10)@RequestParam  String name) {
        return name;
    }

    @GetMapping("/test")
    @ResponseBody
    public String convert(@RequestParam("ids") List<String> ids) {
        return ids.toString();
    }

    @GetMapping("/convertObj")
    @ResponseBody
    public String convertObj(@Nullable Data data) {
        return data.toString();
    }

    static class Data {
        private int age;
        private String name;

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Data{" +
                    "age=" + age +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
}
